package com.eshop.modules.shop.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import java.io.Serializable;
import java.util.Arrays;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.product.domain.StoreProductRelation;
import org.springframework.http.HttpStatus;
import com.eshop.domain.PageResult;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import com.eshop.modules.product.service.dto.StoreProductRelationDto;
import com.eshop.modules.product.service.dto.StoreProductRelationQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.product.service.StoreProductRelationService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "ProductRelation管理" })
@RestController
@RequestMapping({ "/api/yxStoreProductRelation" })
public class YxStoreProductRelationController
{
    private final StoreProductRelationService storeProductRelationService;
    private final IGenerator generator;
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','yxStoreProductRelation:list')")
    public void download(final HttpServletResponse response, final StoreProductRelationQueryCriteria criteria) throws IOException {
        this.storeProductRelationService.download(this.generator.convert(this.storeProductRelationService.queryAll(criteria), (Class)StoreProductRelationDto.class), response);
    }
    
    @GetMapping
    @Log("查询ProductRelation")
    @ApiOperation("查询ProductRelation")
    @PreAuthorize("@el.check('admin','yxStoreProductRelation:list')")
    public ResponseEntity<PageResult<StoreProductRelationDto>> getYxStoreProductRelations(final StoreProductRelationQueryCriteria criteria, final Pageable pageable) {
        return (ResponseEntity<PageResult<StoreProductRelationDto>>)new ResponseEntity(this.storeProductRelationService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @PostMapping
    @Log("新增ProductRelation")
    @ApiOperation("新增ProductRelation")
    @PreAuthorize("@el.check('admin','yxStoreProductRelation:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final StoreProductRelation resources) {
        return new ResponseEntity(this.storeProductRelationService.save(resources), HttpStatus.CREATED);
    }
    
    @PutMapping
    @Log("修改ProductRelation")
    @ApiOperation("修改ProductRelation")
    @PreAuthorize("@el.check('admin','yxStoreProductRelation:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final StoreProductRelation resources) {
        this.storeProductRelationService.updateById(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除ProductRelation")
    @ApiOperation("删除ProductRelation")
    @PreAuthorize("@el.check('admin','yxStoreProductRelation:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Long[] ids) {
        Arrays.asList(ids).forEach(id -> this.storeProductRelationService.removeById((Serializable)id));
        return new ResponseEntity(HttpStatus.OK);
    }
    
    public YxStoreProductRelationController(final StoreProductRelationService storeProductRelationService, final IGenerator generator) {
        this.storeProductRelationService = storeProductRelationService;
        this.generator = generator;
    }
}
